<!--
 * @Author: panghu chenjh@datamargin.com
 * @Date: 2024-04-18 16:54:28
 * @LastEditors: panghu chenjh@datamargin.com
 * @LastEditTime: 2024-04-25 14:29:04
 * @FilePath: \zheye\src\components\Message.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <Teleport to="#message">
    <div v-if="isVisible" class="alert message-info justify-content-between fixed-top w-50 mx-auto d-flex mt-20" :class="[className]" role="alert">
      <div>{{ message }}</div>
      <button type="button" class="close" data-dismiss="alert" @click.prevent="hide">
        x
      </button>
    </div>
  </Teleport>
</template>

<script lang="ts">
import { computed, defineComponent, PropType, ref } from 'vue'
import useDomCreate from '@/hooks/useDomCreate'
export type MessageType = 'success' | 'error' | 'primary'

export default defineComponent({
  name: 'MessageComp',
  props: {
    message: String,
    type: {
      type: String as PropType<MessageType>,
      default: 'primary'
    }
  },
  emits: ['close-message'],
  setup (props, context) {
    useDomCreate('message')
    const className = computed(() => {
      let name = 'alert-primary'
      switch (props.type) {
        case 'error':
          name = 'alert-danger'
          break
        case 'success':
          name = 'alert-success'
          break
        case 'primary':
          name = 'alert-primary'
          break
      }
      return name
    })
    const isVisible = ref(true)
    const hide = () => {
      isVisible.value = false
      context.emit('close-message', true)
    }
    return {
      isVisible,
      hide,
      className
    }
  }
})
</script>

<style scoped>
.mt-20 {
  margin-top: 20px;
}
</style>
